package com.chengh.nami.system.service.impl;

import com.chengh.nami.common.domain.model.LoginUserEntity;
import com.chengh.nami.common.utils.StringUtils;
import com.chengh.nami.system.domain.SysUserOnline;
import com.chengh.nami.system.service.ISysUserOnlineService;
import org.springframework.stereotype.Service;

@Service
public class SysUserOnlineServiceImpl implements ISysUserOnlineService {
    @Override
    public SysUserOnline loginUserEntityToUserOnline(LoginUserEntity userEntity) {
        if (StringUtils.isNull(userEntity) || StringUtils.isNull(userEntity.getUser())) {
            return null;
        }
        SysUserOnline sysUserOnline = new SysUserOnline();
        sysUserOnline.setTokenId(userEntity.getToken());
        sysUserOnline.setUserName(userEntity.getUsername());
        sysUserOnline.setIpaddr(userEntity.getIpaddr());
        sysUserOnline.setLoginLocation(userEntity.getLoginLocation());
        sysUserOnline.setBrowser(userEntity.getBrowser());
        sysUserOnline.setOs(userEntity.getOs());
        sysUserOnline.setLoginTime(userEntity.getLoginTime());
        /*if (StringUtils.isNotEmpty(userEntity.getUser().get)) {
            sysUserOnline.setDeptName(user.getUser().getDept().getDeptName());
        }*/
        return sysUserOnline;
    }

    @Override
    public SysUserOnline selectOnlineByInfo(String ipaddr, String userName, LoginUserEntity userEntity) {
        if (StringUtils.equals(ipaddr, userEntity.getIpaddr()) && StringUtils.equals(userName, userEntity.getUsername())) {
            return loginUserEntityToUserOnline(userEntity);
        }
        return null;
    }

    @Override
    public SysUserOnline selectOnlineByIpaddr(String ipaddr, LoginUserEntity userEntity) {
        if (StringUtils.equals(ipaddr, userEntity.getIpaddr())) {
            return loginUserEntityToUserOnline(userEntity);
        }
        return null;
    }

    @Override
    public SysUserOnline selectOnlineByUserName(String userName, LoginUserEntity userEntity) {
        if (StringUtils.equals(userName, userEntity.getUsername())) {
            return loginUserEntityToUserOnline(userEntity);
        }
        return null;
    }
}
